import datetime
import pytz

import coin.exchange.base.kr_rest.native_public_client_base as npubcb
from coin.exchange.bybit.kr_rest.constants import (inverse_symbol_list, usdt_symbol_list)


class BybitNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self):
    npubcb.NativePublicClientBase.__init__(self, url='')
    self._valid_kline_period = [
        '1', '3', '5', '15', '30', '60', '120', '240', '360', '720', 'D', 'M', 'W']

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
      'Invalid kline period! %s' % kline_period

  def get_history_kline(self, symbol, kline_period, start_time):
    self.check_kline_period(kline_period)
    params = {'symbol': symbol, 'interval': kline_period,
              'from': int(start_time.timestamp())}
    if symbol in inverse_symbol_list:
      url = 'https://api.bybit.com/v2/public/kline/list'
    elif symbol in usdt_symbol_list:
      url = 'https://api.bybit.com/public/linear/kline'
    else:
      raise ValueError('Unknown symbol: %s' % symbol)
    return self._get_as_json(url, params=params)

  def queryAll(self):
    raise NotImplementedError()


if __name__ == "__main__":
  client = BybitNativePublicClient()
  start_time = datetime.datetime(2021, 2, 7).replace(tzinfo=pytz.UTC)
  print(client.get_history_kline('BTCUSD', '60', start_time, end_time).msg)
  print(client.get_history_kline('BTCUSDT', '60', start_time, end_time).msg)
